package com.boot.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 网关配置跨域
 */
@Configuration
public class MallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        //1.允许所有的请求体
        configuration.addAllowedHeader("*");
        //2.允许所有的请求方法
        configuration.addAllowedMethod("*");
        //3.允许所有的来源
        configuration.addAllowedOrigin("*");
        //4.允许携带cookie信息跨域
        configuration.setAllowCredentials(true);
        configurationSource.registerCorsConfiguration("/**",configuration);
        return new CorsWebFilter(configurationSource);
    }

}
